<?php
declare (strict_types=1);

namespace app\job\test;

use app\job\BaseJob;
use think\queue\Job;

/**
 * 执行多个任务的消息队列
 * php think queue:work --queue rxJobSecond
 */
class OrderConsumer extends BaseJob
{
    /**
     * 财务系统消费
     */
    public function taskFinance(Job $job, array $data)
    {
        if ($this->doRunFinanceJob($data)) {
            $job->delete();
            return;
        }

        if ($job->attempts() >= 3) {
            $job->delete();
            echo "任务已执行失败 " . $job->attempts() . " 次,已从队列中删除 \n";
        }
    }

    private function doRunFinanceJob(array $data): bool
    {
        P('执行财务相关逻辑...' . $data['str']);
        return true;
    }

    /**
     * 物流系统消费
     */
    public function taskLogistics(Job $job, array $data)
    {
        if ($this->doRunLogisticsJob($data)) {
            $job->delete();
            return;
        }

        if ($job->attempts() >= 3) {
            $job->delete();
            echo "任务已执行失败 " . $job->attempts() . " 次,已从队列中删除 \n";
        }
    }

    private function doRunLogisticsJob(array $data): bool
    {
        P('执行物流相关逻辑...' . $data['str']);
        return true;
    }
}
